Gomoku MultiPlay Client

MultiPlayForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Client
{
public partial class MultiPlayForm : Form
{
private Thread thread; //
private TcpClient tcpClient;// TCP
private NetworkStream stream;
private const int rectSize = 33; //
private const int edgeCount = 15; //
private enum Horse { none = 0, BLACK, WHITE };
private Horse[,] board;
private Horse nowPlayer;
private bool nowTurn;
private bool playing;
private bool entered;
private bool threading;
private bool judge(Horse Player) //
{
for (int i = 0; i < edgeCount - 4; i++) //
for (int j = 0; j < edgeCount; j++)
if (board[i, j] == Player && board[i + 1, j] == Player && board[i + 2, j] == Player &&
board[i + 3, j] == Player && board[i + 4, j] == Player)
return true;
for (int i = 0; i < edgeCount; i++) //
for (int j = 4; j < edgeCount; j++)
if (board[i, j] == Player && board[i, j - 1] == Player && board[i, j - 2] == Player &&
board[i, j - 3] == Player && board[i, j - 4] == Player)
return true;
for (int i = 0; i < edgeCount - 4; i++) // Y = X
for (int j = 0; j < edgeCount - 4; j++)
if (board[i, j] == Player && board[i + 1, j + 1] == Player && board[i + 2, j + 2] == Player &&
board[i + 3, j + 3] == Player && board[i + 4, j + 4] == Player)
return true;
for (int i = 4; i < edgeCount; i++) // Y = -X
for (int j = 0; j < edgeCount - 4; j++)
if (board[i, j] == Player && board[i - 1, j + 1] == Player && board[i - 2, j + 2] == Player &&
board[i - 3, j + 3] == Player && board[i - 4, j + 4] == Player)
return true;
return false;
}
private void refresh()
{
this.boardPicture.Refresh();
for (int i = 0; i < edgeCount; i++)
for (int j = 0; j < edgeCount; j++)
board[i, j] = Horse.none;
playButton.Enabled = false;
}
private void playButton_Click(object sender, EventArgs e)
{
if (!playing)
{
refresh();
playing = true;
string message = "[Play]";
byte[] buf = Encoding.ASCII.GetBytes(message + this.roomTextBox.Text);
stream.Write(buf, 0, buf.Length);
this.status.Text = " .";
this.playButton.Enabled = false;
}
}
public MultiPlayForm()
{
InitializeComponent();
this.playButton.Enabled = false;
playing = false;
entered = false;
threading = false;
board = new Horse[edgeCount, edgeCount];
nowTurn = false;
}
private void enterButton_Click(object sender, EventArgs e)
{
tcpClient = new TcpClient();
tcpClient.Connect("127.0.0.1", 9876);
stream = tcpClient.GetStream();
thread = new Thread(new ThreadStart(read));
thread.Start();
threading = true;
/* */
string message = "[Enter]";
byte[] buf = Encoding.ASCII.GetBytes(message + this.roomTextBox.Text);
stream.Write(buf, 0, buf.Length);
}
/* . */
private void read()
{
while (true)
{
byte[] buf = new byte[1024];
int bufBytes = stream.Read(buf, 0, buf.Length);
string message = Encoding.ASCII.GetString(buf, 0, bufBytes);
/* (: [Enter]) */
if (message.Contains("[Enter]"))
{
this.status.Text = "[" + this.roomTextBox.Text + "] .";
/* */
this.roomTextBox.Enabled = false;
this.enterButton.Enabled = false;
entered = true;
}
/* (: [Full]) */
if (message.Contains("[Full]"))
{
this.status.Text = " .";
closeNetwork();
}
/* (: [Play]{Horse}) */
if (message.Contains("[Play]"))
{
refresh();
string horse = message.Split(']')[1];
if (horse.Contains("Black"))
{
this.status.Text = " .";
nowTurn = true;
nowPlayer = Horse.BLACK;
}
else
{
this.status.Text = " .";
nowTurn = false;
nowPlayer = Horse.WHITE;
}
playing = true;
}
/* (: [Exit]) */
if (message.Contains("[Exit]"))
{
this.status.Text = " .";
refresh();
}
/* (: [Put]{X,Y}) */
if (message.Contains("[Put]"))
{
string position = message.Split(']')[1];
int x = Convert.ToInt32(position.Split(',')[0]);
int y = Convert.ToInt32(position.Split(',')[1]);
Horse enemyPlayer = Horse.none;
if (nowPlayer == Horse.BLACK)
{
enemyPlayer = Horse.WHITE;
}
else
{
enemyPlayer = Horse.BLACK;
}
if (board[x, y] != Horse.none) continue;
board[x, y] = enemyPlayer;
Graphics g = this.boardPicture.CreateGraphics();
if (enemyPlayer == Horse.BLACK)
{
SolidBrush brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
else
{
SolidBrush brush = new SolidBrush(Color.White);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
if (judge(enemyPlayer))
{
status.Text = ".";
playing = false;
playButton.Text = "";
playButton.Enabled = true;
}
else
{
status.Text = " .";
}
nowTurn = true;
}
}
}
private void boardPicture_MouseDown(object sender, MouseEventArgs e)
{
if (!playing)
{
MessageBox.Show(" .");
return;
}
if (!nowTurn)
{
return;
}
Graphics g = this.boardPicture.CreateGraphics();
int x = e.X / rectSize;
int y = e.Y / rectSize;
if (x < 0 || y < 0 || x >= edgeCount || y >= edgeCount)
{
MessageBox.Show(" .");
return;
}
if (board[x, y] != Horse.none) return;
board[x, y] = nowPlayer;
if (nowPlayer == Horse.BLACK)
{
SolidBrush brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
else
{
SolidBrush brush = new SolidBrush(Color.White);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
/* */
string message = "[Put]" + roomTextBox.Text + "," + x + "," + y;
byte[] buf = Encoding.ASCII.GetBytes(message);
stream.Write(buf, 0, buf.Length);
/* */
if (judge(nowPlayer))
{
status.Text = ".";
playing = false;
playButton.Text = "";
playButton.Enabled = true;
return;
}
else
{
status.Text = " .";
}
/* */
nowTurn = false;
}
private void boardPicture_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
Color lineColor = Color.Black; //
Pen p = new Pen(lineColor, 2);
gp.DrawLine(p, rectSize / 2, rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2); //
gp.DrawLine(p, rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize / 2); //
gp.DrawLine(p, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2); //
gp.DrawLine(p, rectSize * edgeCount - rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2); //
p = new Pen(lineColor, 1);
//
for (int i = rectSize + rectSize / 2; i < rectSize * edgeCount - rectSize / 2; i += rectSize)
{
gp.DrawLine(p, rectSize / 2, i, rectSize * edgeCount - rectSize / 2, i);
gp.DrawLine(p, i, rectSize / 2, i, rectSize * edgeCount - rectSize / 2);
}
}
private void MultiPlayForm_FormClosed(object sender, FormClosedEventArgs e)
{
closeNetwork();
}
void closeNetwork()
{
if (threading && thread.IsAlive) thread.Abort();
if (entered)
{
tcpClient.Close();
}
}
}
}